<?php

class Fan_base extends Base_fetch {

	public function fetch_slightech_fans() {
		return array('slightech_fans' => $this->_parse_fans('http://weibo.cn/u/3848659065'));
	}

	public function fetch_lpang_fans() {
		return array('lpang_fans' => $this->_parse_fans('http://weibo.cn/u/1643944167'));
	}

	public function fetch_jack_fans() {
		return array('jack_fans' => $this->_parse_fans('http://weibo.cn/u/1642327285'));
	}

	public function fetch_mail_list_count() {
		return array('mail_list_count' => Base_fetch::http_get('http://www.jp.slightech.com/maillist/count_all'));
	}

	public function fetch_wechat_fans() {
		$token = json_decode(Base_fetch::http_get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx351f992014f2e88d&secret=6393d96aac3c7971d8f32efcac2a05cc'), true);
		$date  = date('Y-m-d', strtotime('-1 day'));
		$param = json_encode(array('begin_date' => $date, 'end_date' => $date));
		$user_cumulate = json_decode(Base_fetch::http_post('https://api.weixin.qq.com/datacube/getusercumulate?access_token=' . $token['access_token'], $param), true);
		return array('wechat_fans' => $user_cumulate['list'][0]['cumulate_user']);
	}

	public function fetch_slightech_account() {
		return array('slightech_account' => Base_fetch::http_get('http://api.slightech.com/user/count_all'));
	}

	private function _parse_fans($url) {
		$html = Base_fetch::http_get($url);
		if (!empty($html)) {
			$matches = array();
			if (preg_match('/粉丝\[(\d*)\]/', $html, $matches)) {
				return $matches[1];
			}
		}
		return 0;
	}

}